<?php
namespace app\admin\logic;
use think\Facade\Db;
use app\common\Apiv;
/**
 * @Author: K.EL
 * @Date:   2020-07-20 23:13:04
 * @Last Modified by:   K.EL
 * @Last Modified time: 2020-07-22 18:27:08
 */
class RoleLogic{
	static function getLists(){
		$role_name = input('role_name', '');
		$status = input('status', '');
		$pageSize = input('size', 10);
		$obj = Db::name('sys_role')
					->field('*,CAST(status AS CHAR) as status');
		if($role_name){
			$obj->where('role_name','like','%'.$role_name.'%');
		}
		if($status){
			$obj->where('status', $status);
		}else{
			$obj->where('status','exp','>0');
		}
		$lists = $obj->order('id desc')->paginate($pageSize);
		return Apiv::result(1, 0, '信息获取成功', $lists);
	}

	static function getListsNoPage(){
		$role_name = input('role_name', '');
		$status = input('status', '');
		$obj = Db::name('sys_role')
					->field('id,role_name,status');
		if($role_name){
			$obj->where('role_name','like','%'.$role_name.'%');
		}
		if($status){
			$obj->where('status', $status);
		}else{
			$obj->where('status','exp','>0');
		}
		$lists = $obj->order('id desc')->select();
		return Apiv::result(1, 0, '信息获取成功', $lists);
	}

	static function add(){
		$data = [];
		$data['role_name'] = input('role_name');
		$data['status'] = input('status', 1);
		$data['add_time'] = date('Y-m-d H:i:s');
		$id = Db::name('sys_role')->insertGetId($data);
		$data['id'] = $id;
		return Apiv::result(1, 0, '新增成功', $data);
	}

	static function edit(){
		$data = [];
		$data['role_name'] = input('role_name');
		$data['status'] = input('status', 1);
		$id = input('id',0);
		Db::name('sys_role')->where('id', $id)->update($data);
		$data['id'] = $id;
		return Apiv::result(1, 0, '编辑成功', $data);
	}

	static function toAuth(){
		$role_id = input('role_id', 0);
		$menu_ids = input('menu_ids','');
		$role_menu_ids = implode(',', $menu_ids);
		Db::name('sys_role')->where('id', $role_id)->update(['role_menu_ids'=>$role_menu_ids]);
		return Apiv::result(1, 0, '授权成功');
	}
}
